<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车位标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入车位标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.parking_spot_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="GPS纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入GPS纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="GPS经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入GPS经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在建筑" prop="buildingName">
        <el-input
          v-model="queryParams.buildingName"
          placeholder="请输入所在建筑"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入所在楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车位编号" prop="parkingNumber">
        <el-input
          v-model="queryParams.parkingNumber"
          placeholder="请输入车位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可充电" prop="isEvCharging">
        <el-select v-model="queryParams.isEvCharging" placeholder="请选择可充电" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="月租价格" prop="pricePerMonth">
        <el-input
          v-model="queryParams.pricePerMonth"
          placeholder="请输入月租价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最短租期" prop="minRentalPeriod">
        <el-input
          v-model="queryParams.minRentalPeriod"
          placeholder="请输入最短租期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格可议" prop="isNegotiable">
        <el-select v-model="queryParams.isNegotiable" placeholder="请选择价格可议" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['parking:spots:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['parking:spots:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['parking:spots:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parking:spots:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spotsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="车位标题" align="center" prop="title" />
      <el-table-column label="车位描述" align="center" prop="description" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.parking_spot_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="GPS纬度" align="center" prop="latitude" />
      <el-table-column label="GPS经度" align="center" prop="longitude" />
      <el-table-column label="所在建筑" align="center" prop="buildingName" />
      <el-table-column label="所在楼层" align="center" prop="floor" />
      <el-table-column label="车位编号" align="center" prop="parkingNumber" />
      <el-table-column label="可充电" align="center" prop="isEvCharging">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isEvCharging"/>
        </template>
      </el-table-column>
      <el-table-column label="月租价格" align="center" prop="pricePerMonth" />
      <el-table-column label="最短租期" align="center" prop="minRentalPeriod" />
      <el-table-column label="价格可议" align="center" prop="isNegotiable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isNegotiable"/>
        </template>
      </el-table-column>
      <el-table-column label="车位照片" align="center" prop="photoUrls" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['parking:spots:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['parking:spots:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车位出租对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车位标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入车位标题" />
        </el-form-item>
        <el-form-item label="车位描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.parking_spot_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="GPS纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入GPS纬度" />
        </el-form-item>
        <el-form-item label="GPS经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入GPS经度" />
        </el-form-item>
        <el-form-item label="所在建筑" prop="buildingName">
          <el-input v-model="form.buildingName" placeholder="请输入所在建筑" />
        </el-form-item>
        <el-form-item label="所在楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入所在楼层" />
        </el-form-item>
        <el-form-item label="车位编号" prop="parkingNumber">
          <el-input v-model="form.parkingNumber" placeholder="请输入车位编号" />
        </el-form-item>
        <el-form-item label="可充电" prop="isEvCharging">
          <el-select v-model="form.isEvCharging" placeholder="请选择可充电">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="月租价格" prop="pricePerMonth">
          <el-input v-model="form.pricePerMonth" placeholder="请输入月租价格" />
        </el-form-item>
        <el-form-item label="最短租期" prop="minRentalPeriod">
          <el-input v-model="form.minRentalPeriod" placeholder="请输入最短租期" />
        </el-form-item>
        <el-form-item label="价格可议" prop="isNegotiable">
          <el-select v-model="form.isNegotiable" placeholder="请选择价格可议">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车位照片" prop="photoUrls">
          <el-input v-model="form.photoUrls" placeholder="请输入车位照片" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpots, getSpots, delSpots, addSpots, updateSpots } from "@/api/parking/spots";

export default {
  name: "Spots",
  dicts: ['sys_yes_no', 'parking_spot_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位出租表格数据
      spotsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        description: null,
        status: null,
        address: null,
        latitude: null,
        longitude: null,
        buildingName: null,
        floor: null,
        parkingNumber: null,
        isEvCharging: null,
        pricePerMonth: null,
        minRentalPeriod: null,
        isNegotiable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "车位标题不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        pricePerMonth: [
          { required: true, message: "月租价格不能为空", trigger: "blur" }
        ],
        minRentalPeriod: [
          { required: true, message: "最短租期不能为空", trigger: "blur" }
        ],
        isNegotiable: [
          { required: true, message: "价格可议不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车位出租列表 */
    getList() {
      this.loading = true;
      listSpots(this.queryParams).then(response => {
        this.spotsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        createTime: null,
        updateTime: null,
        status: null,
        address: null,
        latitude: null,
        longitude: null,
        buildingName: null,
        floor: null,
        parkingNumber: null,
        isEvCharging: null,
        pricePerMonth: null,
        minRentalPeriod: null,
        isNegotiable: null,
        photoUrls: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车位出租";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpots(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车位出租";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpots(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpots(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车位出租编号为"' + ids + '"的数据项？').then(function() {
        return delSpots(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parking/spots/export', {
        ...this.queryParams
      }, `spots_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
